package com.aglhz.nature.view;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



import com.aglhz.base.BaseActivity;
import com.aglhz.nature.R;
import com.aglhz.nature.ListView.MyListView;
import com.aglhz.nature.adapter.AroundAdapter;
import com.aglhz.nature.adapter.AroundSearchAdapter;
import com.aglhz.nature.modle.item.AroundData;
import com.aglhz.nature.modle.item.AroundSearchData;
import com.aglhz.nature.presenter.AroundPresenter;
import com.aglhz.nature.view.iv.AroundView;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.umeng.socialize.utils.Log;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class AroundActivity extends BaseActivity implements AroundView,OnClickListener, OnGestureListener{
//	public  static  double lat;
//	public static  double lon;
	
	//声明AMapLocationClient类对象
	public AMapLocationClient mLocationClient = null;
	//声明定位回调监听器
	public AMapLocationListener mLocationListener =null;
	//声明mLocationOption对象
	public AMapLocationClientOption mLocationOption = null;
	private AroundPresenter presenter;
	private AroundAdapter adapter;
	private MyListView  around_listView;
	private TextView around_location;
	private EditText around_et_input;
	private TextView around_btn_search;
	private LinearLayout view;
	private AroundSearchAdapter madapter;
	Double d;
	Double f;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.around);
		setActionBarTitle("周边商家");
		
		around_listView=(MyListView) findViewById(R.id.around_listView);
		around_location=(TextView) findViewById(R.id.around_location);
		around_et_input=(EditText) findViewById(R.id.around_et_input);
		around_btn_search=(TextView) findViewById(R.id.around_btn_search);
		view=(LinearLayout) findViewById(R.id.view);
		around_location.setOnClickListener(this);
		around_btn_search.setOnClickListener(this);
		presenter = new AroundPresenter(this);
//		d=AroundActivity.lat;
		// basePresenter register
		presenter.register();
		  Intent intent =getIntent();
		  f= intent.getDoubleExtra("lon", 0);
		  d= intent.getDoubleExtra("lat", 0);
		  Log.e("oooo","----------intent------"+d);
		  if(0.0==d&&0.0==f){
			  Log.e("oooo","----------111------");
			  presenter.loadData();
		  }else{
			  Log.e("oooo","--------2222--------");
			  presenter.loadAroundBean(d,f);
		  }
		
	}

	
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.around_location:
			Intent intent =new Intent(AroundActivity.this,PoiKeywordSearchActivity.class);
			startActivity(intent);
			
			break;

		case R.id.around_btn_search:
			
			
			presenter.searchAroundShop(around_et_input.getText().toString().trim());
			
			break;
		default:
			break;
		}
		
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		mLocationClient.onDestroy();//销毁定位客户端
		// basePresenter unregister
		presenter.unregister();
	}

	@Override
	public void showAroundList(List<AroundData> data) {
		adapter = new AroundAdapter(data, AroundActivity.this);
		around_listView.setAdapter(adapter);
		
	}
	
	@Override
	public void showAroundSearchList(List<AroundSearchData> data) {
		madapter = new AroundSearchAdapter(data, AroundActivity.this);
		around_listView.setAdapter(madapter);
	}
	
	
	@Override
	public void getLOcation(){
		//初始化定位
			mLocationClient = new AMapLocationClient(getApplicationContext());
			//设置定位回调监听
			mLocationClient.setLocationListener(new AMapLocationListener(){

				@Override
				public void onLocationChanged(AMapLocation amapLocation) {
					if (amapLocation != null) {
				        if (amapLocation.getErrorCode() == 0) {
				        //定位成功回调信息，设置相关消息
				        amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
				        amapLocation.getLatitude();//获取纬度
				        amapLocation.getLongitude();//获取经度
				        amapLocation.getAccuracy();//获取精度信息
				        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				        Date date = new Date(amapLocation.getTime());
				        df.format(date);//定位时间
				        amapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
				        amapLocation.getCountry();//国家信息
				        amapLocation.getProvince();//省信息
				        amapLocation.getCity();//城市信息
				        amapLocation.getDistrict();//城区信息
				        amapLocation.getStreet();//街道信息
				        amapLocation.getStreetNum();//街道门牌号信息
				        amapLocation.getCityCode();//城市编码
				        amapLocation.getAdCode();//地区编码
				        amapLocation.getAoiName();//获取当前定位点的AOI信息
				        presenter.loadAroundBean( amapLocation.getLatitude(), amapLocation.getLongitude());
				        mLocationClient.stopLocation();//停止定位
				    } else {
				    	   presenter.loadAroundBean();
				    	   mLocationClient.stopLocation();//停止定位
				              //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
				        Log.e("AmapError","location Error, ErrCode:"
				            + amapLocation.getErrorCode() + ", errInfo:"
				            + amapLocation.getErrorInfo());
				        }
				    }
				  }
				
			});
			

			//初始化定位参数
			mLocationOption = new AMapLocationClientOption();
			//设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
			mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
			//设置是否返回地址信息（默认返回地址信息）
			mLocationOption.setNeedAddress(true);
			//设置是否只定位一次,默认为false
			mLocationOption.setOnceLocation(false);
			//设置是否强制刷新WIFI，默认为强制刷新
			mLocationOption.setWifiActiveScan(true);
			//设置是否允许模拟位置,默认为false，不允许模拟位置
			mLocationOption.setMockEnable(false);
			//设置定位间隔,单位毫秒,默认为2000ms
			mLocationOption.setInterval(2000);
			//给定位客户端对象设置定位参数
			mLocationClient.setLocationOption(mLocationOption);
			//启动定位
			mLocationClient.startLocation();
	}


	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}






	

}
